home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageio.lib / dev / examples / read / main.c next >
Encoding:
C/C++ Source or Header  |  2000-08-09  |  6.9 KB  |  286 lines

  1. /* This example use of imageio.library loads the image file specified
  2.         on the command line and views it halved in size in a guigfx.library
  3.         window. It also demonstrates the use of a progress indicator
  4.         callback hook.
  5.  
  6.         Uncomment the display module you need on line 97 (currently guigfx).
  7. */
  8.  
  9. #include <stdio.h>
  10.  
  11. #include <dos/dos.h>
  12. #include <exec/memory.h>
  13. #include <exec/types.h>
  14.  
  15. #include <clib/dos_protos.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/cybergraphics_protos.h>
  18. #include <clib/intuition_protos.h>
  19.  
  20. #include <pragmas/dos_pragmas.h>
  21. #include <pragmas/exec_pragmas.h>
  22. #include <pragmas/intuition_pragmas.h>
  23. #include <pragmas/cybergraphics_pragmas.h>
  24.  
  25. #include <cybergraphics/cybergraphics.h>
  26.  
  27. #include <imageio/imageio.h>
  28. #include <imageio/imageio_protos.h>
  29. #include <imageio/imageio_pragmas.h>
  30.  
  31. #include <guigfx/guigfx.h>
  32. #include <pragmas/guigfx_pragmas.h>
  33. #include <guigfx/guigfx_protos.h>
  34.  
  35. /* Function prototypes */
  36. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  37. void DisplayCyberGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y );
  38. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y );
  39.  
  40. extern struct Library *DOSBase;
  41. struct Library *ImageIOBase, *IntuitionBase;
  42.  
  43. void main( int argc, char **argv )
  44. {
  45.     if ( argv[1] != NULL )
  46.     {
  47.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  48.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  49.         if ( IntuitionBase && ImageIOBase )
  50.         {
  51.             struct ImageHandle *ih;
  52.             ULONG err;
  53.             BPTR fp;
  54.  
  55.             fp = Open( argv[1], MODE_OLDFILE );
  56.             if ( fp != NULL )
  57.             {
  58.                 err = AllocImage( &ih,
  59.                     IMG_SrcFile, fp,
  60.                     TAG_DONE );
  61.                 if ( !err )
  62.                 {
  63.                     ULONG num = 1, denom = 2;
  64.                     ULONG x, y, bpp, rs;
  65.                     UBYTE *buffer, cs, it;
  66.  
  67.                     err = GetImageAttrs( ih,
  68.                         IMG_ImageType, &it,
  69.                         TAG_DONE );
  70.                     if ( !err )
  71.                     {
  72.                         printf("Image type=%d\n",it);
  73.                     }
  74.  
  75.                     err = GetImageAttrs( ih,
  76.                         IMG_Width, &x,
  77.                         IMG_Height,&y,
  78.                         IMG_BytesPerPixel, &bpp,
  79.                         IMG_ColourSpace, &cs,
  80.                         IMG_RowSize, &rs,
  81.                         IMG_TestScaleNum, num,
  82.                         IMG_TestScaleDenom, denom,
  83.                         TAG_DONE );
  84.                     if ( !err )
  85.                     {
  86.                         printf( "width=%ld, height=%ld\n", x, y );
  87.                         printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  88.                         printf( "row size=%ld\n", rs );
  89.  
  90.                         err = ReadImage( ih,
  91.                             IMG_ScaleNum, num,
  92.                             IMG_ScaleDenom, denom,
  93.                             IMG_ImageBuffer, &buffer,
  94.                             IMG_ProgressHook, progressFunc,
  95.                             TAG_DONE );
  96.                         if ( !err )
  97.                         {
  98.                             /* Display image  - uncomment the line you need */
  99. //                            DisplayCyberGfx( buffer, cs, rs, x, y );
  100.                             DisplayGuiGfx( buffer, cs, rs, x, y );
  101.                         }
  102.                         else printf( "read image error:%d\n", err );
  103.                     }
  104.                     else printf( "get image attrs error:%d\n", err );
  105.  
  106.                     FreeImage( ih );
  107.                 }
  108.                 else printf( "alloc image error:%d\n", err );
  109.  
  110.                 Close( fp );
  111.             }
  112.             else printf( "cant open file\n" );
  113.         }
  114.  
  115.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  116.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  117.     }
  118.     else printf( "no file specified\n" );
  119. }
  120.  
  121. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  122. {
  123.     static int prevpercent = 0;
  124.  
  125.     int percent = ( curr * 100 ) / lines;
  126.  
  127.     if ( prevpercent != percent )
  128.     {
  129.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  130.     }
  131.  
  132.     prevpercent = percent;
  133.  
  134.     return NULL;
  135. }
  136.  
  137. void DisplayCyberGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y )
  138. {
  139.     struct CyberGfxBase *CyberGfxBase = NULL;
  140.     ULONG DisplayID;
  141.     struct Screen *scr;
  142.     struct Window *win;
  143.     struct Message *msg;
  144.  
  145.     CyberGfxBase = (struct CyberGfxBase *)OpenLibrary( "cybergraphics.library", 0L );
  146.     if ( CyberGfxBase )
  147.     {
  148.         DisplayID = BestCModeIDTags( CYBRBIDTG_NominalWidth, 640,
  149.             CYBRBIDTG_NominalHeight, 480,
  150.             CYBRBIDTG_Depth, 24,
  151.             TAG_DONE );
  152.  
  153.         if ( DisplayID != INVALID_ID )
  154.         {
  155.             scr = OpenScreenTags( NULL,
  156.                 SA_Title, "Proof",
  157.                 SA_DisplayID, DisplayID,
  158.                 SA_Depth, GetCyberIDAttr( CYBRIDATTR_DEPTH, DisplayID ),
  159.                 TAG_DONE );
  160.  
  161.             if ( scr != NULL )
  162.             {
  163.                 win = OpenWindowTags( NULL,
  164.                     WA_Title, "Proof",
  165.                     WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  166.                         WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  167.                         WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  168.                     WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  169.                         IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  170.                     WA_Left, 16,
  171.                     WA_Top, scr->BarHeight+16,
  172.                     WA_Width, x,
  173.                     WA_Height, y,
  174.                     WA_CustomScreen, scr,
  175.                     TAG_DONE );
  176.  
  177.                 if ( win != NULL )
  178.                 {
  179.                     UBYTE format = RECTFMT_RGB;
  180.  
  181.                     switch ( cs )
  182.                     {
  183.                         case IMCS_UNKNOWN:
  184.                         case IMCS_GREY:
  185.                             format = RECTFMT_GREY8;
  186.                         break;
  187.                     }
  188.  
  189.                     WritePixelArray( buffer, 0, 0, rs, win->RPort, 0, 0, x, y, format );
  190.  
  191.                     Wait( 1L << win->UserPort->mp_SigBit );
  192.                     while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  193.  
  194.                     CloseWindow( win );
  195.                 }
  196.                 else printf( "failed to open window\n" );
  197.  
  198.                 CloseScreen( scr );
  199.             }
  200.             else printf( "failed to open screen\n" );
  201.         }
  202.         else printf( "failed to get display id\n" );
  203.     }
  204.     else printf( "failed to open cybergfx.library\n" );
  205.  
  206.     if ( CyberGfxBase ) CloseLibrary ( (struct Library *)CyberGfxBase );
  207. }
  208.  
  209. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y )
  210. {
  211.     struct Library *GuiGFXBase;
  212.  
  213.     GuiGFXBase = OpenLibrary( "guigfx.library", NULL );
  214.     if ( GuiGFXBase )
  215.     {
  216.         struct Window *win;
  217.         APTR dh, pi;
  218.         UBYTE *argb;
  219.  
  220.         win = OpenWindowTags( NULL,
  221.             WA_Title, "Proof",
  222.             WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  223.                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  224.                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  225.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  226.                 IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  227.             WA_Left, 16,
  228.             WA_Top, 16,
  229.             WA_Width, x,
  230.             WA_Height, y,
  231.             TAG_DONE );
  232.  
  233.         if ( win != NULL )
  234.         {
  235.             dh = ObtainDrawHandle( NULL, win->RPort, win->WScreen->ViewPort.ColorMap, TAG_DONE );
  236.  
  237.             if ( dh )
  238.             {
  239.                 argb = AllocVec( x * y * 4, MEMF_PUBLIC | MEMF_CLEAR );
  240.  
  241.                 if ( argb )
  242.                 {
  243.                     int i, count = 0;
  244.                     char **dest;
  245.  
  246.                     /* Convert an RGB buffer to an ARGB buffer setting A to 0.*/
  247.                     dest = (char **)argb;
  248.  
  249.                     for ( i = 0; i < x * y * 3; i += 3 )
  250.                     {
  251.                         dest[count++] = (char *)( ( (ULONG) * (char**)&buffer[i] ) >> 8 );
  252.                     }
  253.  
  254.                     pi = MakePicture( argb, x, y, GGFX_PixelFormat, PIXFMT_0RGB_32, TAG_DONE );
  255.  
  256.                     if ( pi )
  257.                     {
  258.                         struct Message *msg;
  259.  
  260.                         DrawPicture( dh, pi, 0, 0, NULL );
  261.  
  262.                         Wait( 1L << win->UserPort->mp_SigBit );
  263.  
  264.                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  265.  
  266.                         DeletePicture( pi );
  267.                     }
  268.                     else printf( "failed to create picture\n" );
  269.  
  270.                     FreeVec( argb );
  271.                 }
  272.                 else printf( "failed to allocate argb buffer\n" );
  273.  
  274.                 ReleaseDrawHandle( dh );
  275.             }
  276.             else printf( "failed to get drawhandle\n" );
  277.  
  278.             CloseWindow( win );
  279.         }
  280.         else printf( "failed to open window\n" );
  281.     }
  282.     else printf( "failed to open guigfx.library\n" );
  283.  
  284.     if ( GuiGFXBase ) CloseLibrary( GuiGFXBase );
  285. }
  286.